package com.yuexun.dhdoctor.module.Contract.ui;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseEntity;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.Contract.adapter.HospitalSelectAdapter;
import com.yuexun.dhdoctor.module.Contract.bean.ShareHospBean;
import com.yuexun.dhdoctor.module.Contract.model.impl.ShareHospModel;
import com.yuexun.dhdoctor.module.Contract.presenter.impl.ShareHospPresenter;
import com.yuexun.dhdoctor.module.Contract.ui.view.IShareHospView;
import com.yuexun.dhdoctor.module.login.bean.AreaBean;
import com.yuexun.dhdoctor.module.login.bean.AreaHospBean;
import com.yuexun.dhdoctor.widget.EmptyLayout;
import com.yuexun.dhdoctor.widget.PullToRefresh.FooterLoadingLayout;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshBase;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshList;

import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2017/12/1 0001.
 */

public class Act_Hospital_Select extends BaseKJActivity implements IShareHospView {

    @Bind(R.id.lvHospital)
    PullToRefreshList lvHospital;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    List<AreaHospBean> hospitals = new ArrayList<>();
    HospitalSelectAdapter adapter;
    private ListView mList;
    String newHospital;
    int newHospitalId;
    int strHospitalId;//登录的hospId
    ShareHospPresenter presenter;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_hospital_select);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        presenter = new ShareHospPresenter(new ShareHospModel(), this);
//        inquireHospitalList();

        newHospital = getIntent().getExtras().getString("newHospital");
        newHospitalId = getIntent().getExtras().getInt("newHospitalId");
        strHospitalId = getIntent().getExtras().getInt("strHospitalId");

        if (strHospitalId == 1) {
            List<AreaBean> areaBeans = AppConfig.AREA_BEAN;
            if (areaBeans != null && areaBeans.size() > 0) {
                for (AreaBean areaBean : areaBeans) {
                    hospitals.addAll(areaBean.getHospList());
                }
                emptyLayout.dismiss();
            }

        } else {
            inquireHospitalList();
        }

        listViewPreference();
        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setSelectHospital(hospitals.get(position).getId());
                newHospital = hospitals.get(position).getHospitalName();
                newHospitalId = hospitals.get(position).getId();
                finishView();
            }
        });

        adapter = new HospitalSelectAdapter(mList, hospitals, R.layout.item_hospital_select);
        mList.setAdapter(adapter);
        adapter.setSelectHospital(newHospitalId);
    }

    private void inquireHospitalList() {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        presenter.inquireShareHospList(params);
    }

    /**
     * 初始化ListView样式
     */
    private void listViewPreference() {
        mList = lvHospital.getRefreshView();
        mList.setDivider(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        mList.setOverscrollFooter(null);
        mList.setOverscrollHeader(null);
        mList.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        lvHospital.setPullLoadEnabled(false);//设置上拉刷新功能关闭
        ((FooterLoadingLayout) lvHospital.getFooterLoadingLayout()).setNoMoreDataText("已经是最新状态");

        lvHospital.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                inquireHospitalList();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
            }
        });

        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                inquireHospitalList();
            }
        });
    }

    @OnClick({R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finishView();
                break;
        }
    }

    private void finishView() {
        Bundle bundle = new Bundle();
        Intent intent = new Intent();
        intent.setAction("com.MyBroadcastReceiver");
        bundle.putString("hospitalName", newHospital);
        bundle.putInt("hospitalId", newHospitalId);
        intent.putExtras(bundle);
        sendBroadcast(intent);
        finish();
    }

    @Override
    public void getHospList(BaseEntity<ShareHospBean> baseEntity) {
        if (lvHospital.isPullRefreshEnabled())
            lvHospital.onPullDownRefreshComplete();
        if (baseEntity.getDatas().size() < 999) {
            lvHospital.setHasMoreData(false);
        } else {
            if (lvHospital.isPullLoadEnabled())
                lvHospital.onPullUpRefreshComplete();
        }
        if (baseEntity.getStatus().equals("200")) {
            hospitals.clear();
            for (ShareHospBean shareHospBean : baseEntity.getDatas()) {
                AreaHospBean hospBean = new AreaHospBean();
                hospBean.setHospitalName(shareHospBean.getHospName());
                hospBean.setId(shareHospBean.getHospId());
                hospitals.add(hospBean);
            }
            adapter.refresh(hospitals);
            emptyLayout.dismiss();
        } else {
            emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
        }
    }
}
